const app = getApp()
import { waterfallCalc } from "@/utils/utils";
import Api from '@/services/api.js';
export default {
  props: {
    navigateKey: {
      type: Number,
      default: 0,
    },
    calcHeight: {
      type: Boolean,
      default: false
    },
    itemHeight: {
      type: Number,
      default: 578
    },
    page: {
      type: String,
      default: 'home'
    },
    list: {
      type: Array,
      default: []
    },
    // 额外参数  游记需要 返回的结构的产物
    likeNumObj: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      values: [],
      newList: [],
      defaultValue: this.value,
      maxHeight: 0,
    }
  },
  // watch: {
  //   navigateKey() {
  //     if (this.calcHeight && this.navigateKey == 0) {
  //       this.$emit('complete', { totalHeight: this.maxHeight })
  //     }
  //   }
  // },
  watch: {
    list(val) {
      console.log('============>', val);
      if (val) {
        const { arr, maxHeight } = waterfallCalc(val.map(i => {
          i.height = this.itemHeight / 2
          return i
        }))
        this.newList = arr;
        console.log('=============>', this.newList);
      }
    }
  },
  computed: {
    privacyData() {
      return this.$store.state.privacyData;
    }
  },
  mounted() {
    // setTimeout(()=>{
    // const { arr, maxHeight } = waterfallCalc(this.list.map(i => {
    //   i.height = this.itemHeight / 2
    //   return i
    // }))
    // this.newList = arr;

    // this.maxHeight = maxHeight + 17* this.newList[0].length * 2;

    // this.$emit('complete', { totalHeight: this.maxHeight })

  },
  methods: {
    
    onItemClick(e) {
     this.$emit('tapItem',  e.currentTarget.dataset.item);
    },
    // 收藏游记
    async handleLike(e) {
      const { parentIndex, index, id } = e.currentTarget.dataset;
      let selfZan = this.newList[parentIndex][index].selfZan;
      const { suc, result } = await this.$request({
        url: selfZan ?  Api.travelLoguelikeCancel : Api.travelLoguelike,
        data: {
          userId: this.privacyData.userId,
          noteId: id
        }
      })
      if (suc) {
        this.newList[parentIndex][index].selfZan = !selfZan;
        if(selfZan){
          this.likeNumObj[id] = this.likeNumObj[id]-1;
        }else{
          this.likeNumObj[id] = this.likeNumObj[id]-(-1);
        }
      }
    }
  }
}